package com.sunshine.controller;

import com.sunshine.bean.Information;
import com.sunshine.constant.StatusCode;
import com.sunshine.model.InformationMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static com.sunshine.bean.Information.TYPE.PRAISE;
import static com.sunshine.util.Result.json;

/**
 * Created by along on 11/18/17.
 */
@RestController
@RequestMapping("/api/information")
public class InformationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(InformationController.class);

    @Autowired
    private InformationMapper informationMapper;

    @GetMapping
    public Map<String, Object> show(@RequestParam(value = "id") String id) {
        Information information = informationMapper.findById(id);
        if (information == null) {
            return json(StatusCode.SYSTEM_ERROR);
        } else {
            Map<String, Object> item = new HashMap<>();
            item.put("id", information.getId());
            item.put("title", information.getTitle());
            item.put("content",information.getContent());
            item.put("created_date", information.getCreatedDate());
            item.put("status", information.getStatus());
            item.put("location",information.getLocation());
            item.put("conductor",information.getConductor());
            item.put("response_date",information.getResponseDate());
            item.put("response",information.getResponse());
            item.put("type",information.getType());
            return json(StatusCode.SUCCESS, item);
        }
    }

    @PostMapping
    public Map<String, Object> create(@RequestParam(value = "type") String type,
                                      @RequestParam(value = "name") String name,
                                      @RequestParam(value = "contact", required = false) String contact,
                                      @RequestParam(value = "title") String title,
                                      @RequestParam(value = "location", required = false) String location,
                                      @RequestParam(value = "content") String content,
                                      @RequestParam(value = "public") int publical,
                                      @RequestParam(value = "attachment", required = false) String attachment) {
        Information information = new Information();
        String id = new SimpleDateFormat("yyyyMMddHHmmss").format(new Timestamp(System.currentTimeMillis()));
        information.setType(type);
        information.setName(name);
        information.setTitle(title);
        information.setLocation(location);
        information.setContent(content);
        information.setPublical(publical == 1);
        information.setAttachment(attachment);
        information.setStatus(Information.STATUS.WAITING.toString());
        information.setId(id);
        informationMapper.insert(information);
        LOGGER.info(information.toString());
        return json(StatusCode.SUCCESS, "id", information.getId());
    }

    @GetMapping("/public")
    public Map<String, Object> openList() {
        List<Information> informationList = informationMapper.findPublic();
        return putList(informationList);
    }

    @GetMapping("/praise")
    public Map<String, Object> praiseList() {
        List<Information> informationList = informationMapper.findByType(PRAISE.toString());
        return putList(informationList);
    }

    @GetMapping("/praise/preview")
    public Map<String, Object> praiseListPreview() {
        List<Information> informationList = informationMapper.previewByType(PRAISE.toString(),5);
        return putList(informationList);
    }

    @GetMapping("/search")
    public Map<String, Object> search(@RequestParam(value = "id") String id) {
        Information information = informationMapper.findById(id);
        if (information == null) {
            return json(StatusCode.SUCCESS);
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("id", information.getId());
            result.put("title", information.getTitle());
            result.put("created_date", information.getCreatedDate());
            result.put("status", information.getStatus());
            return json(StatusCode.SUCCESS, result);
        }
    }

    private Map<String, Object> putList(List<Information> informationList) {
        List<Map<String, Object>> results = new LinkedList<>();
        if (informationList == null) {
            return json(StatusCode.SUCCESS);
        } else {
            for (Information information : informationList) {
                Map<String, Object> item = new HashMap<>();
                item.put("id", information.getId());
                item.put("title", information.getTitle());
                item.put("created_date", information.getCreatedDate());
                item.put("status", information.getStatus());
                results.add(item);
            }
            return json(StatusCode.SUCCESS, results);
        }
    }
}
